package com.mytaobao.main.firsttab.func.headline;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.baseproject.view.banner.VerticalViewPager;
import com.mytaobao.R;
import com.mytaobao.main.MainActivity;


public class HeadLineVerticalViewPager extends LinearLayout{
    private static final String TAG = "HeadLineVerticalViewPager";

    private static final float MIN_SCALE = 0.75f;
    private static final float MIN_ALPHA = 0.75f;

    private static final int RMP = LayoutParams.MATCH_PARENT;
    private static final int RWP = LayoutParams.WRAP_CONTENT;
    private static final int LWC = LinearLayout.LayoutParams.WRAP_CONTENT;
    private static final int WHAT_AUTO_PLAY = 1000;

    private VerticalViewPager mVerticalViewPager;
    //是否是网络图片
    private boolean mIsImageUrl = false;
    //是否只有一张图片
    private boolean mIsOneImg = false;
    //是否可以自动播放
    private boolean mAutoPlayAble = true;
    //是否正在播放
    private boolean mIsAutoPlaying = false;
    //自动播放时间
    private int mAutoPalyTime = 2000;
    //当前页面位置
    private int mCurrentPositon;

    private Handler mAutoPlayHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (((MainActivity)mAppCompatActivity).getFirstTabFragment().getRVScrollState() == RecyclerView.SCROLL_STATE_IDLE) {
                mCurrentPositon++;
                mVerticalViewPager.setCurrentItem(mCurrentPositon);
            }
            mAutoPlayHandler.sendEmptyMessageDelayed(WHAT_AUTO_PLAY, mAutoPalyTime);
        }
    };

    //监听home键和长按home键
    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == View.INVISIBLE) {
            stopAutoPlay();
        } else if(visibility == View.VISIBLE){
            startAutoPlay();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopAutoPlay();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startAutoPlay();
    }

    public HeadLineVerticalViewPager(Context context) {
        this(context, null);
    }

    public HeadLineVerticalViewPager(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeadLineVerticalViewPager(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        setLayout(context);
    }

    private void setLayout(Context context) {
        //关闭view的OverScroll
        setOverScrollMode(OVER_SCROLL_NEVER);
    }

    private AppCompatActivity mAppCompatActivity;

    public void setData(AppCompatActivity appCompatActivity) {
        this.mAppCompatActivity = appCompatActivity;
        initViewPager();
    }

    HeadlineAdapter mHeadlineAdapter;

    private void initViewPager() {
        //添加ViewPager
        mVerticalViewPager = (VerticalViewPager) findViewById(R.id.vvp);
        mHeadlineAdapter = new HeadlineAdapter(mAppCompatActivity.getSupportFragmentManager());
        mVerticalViewPager.setAdapter(mHeadlineAdapter);
        mVerticalViewPager.setOnPageChangeListener(mOnPageChangeListener);
        //跳转到首页
        mVerticalViewPager.setCurrentItem(1, false);
        //当图片多于1张时开始轮播
        startAutoPlay();
    }

    private ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            mCurrentPositon = position % (mVerticalViewPager.getAdapter().getCount());
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (state == ViewPager.SCROLL_STATE_IDLE) {
                int current = mVerticalViewPager.getCurrentItem();
                int lastReal = mVerticalViewPager.getAdapter().getCount() - 2;
                if (current == 0) {//如果走到第一幅image左边的空白item，就显示最后一幅有效image
                    mVerticalViewPager.setCurrentItem(lastReal, false);
                } else if (current == lastReal + 1) {//如果走到最后一幅image右边的空白item，就显示第一幅有效image
                    mVerticalViewPager.setCurrentItem(1, false);
                }
            }
        }
    };

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mAutoPlayAble && !mIsOneImg) {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    stopAutoPlay();
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_OUTSIDE:
                    startAutoPlay();
                    break;
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 开始播放
     */
    public void startAutoPlay() {
        if (mAutoPlayAble && !mIsAutoPlaying) {
            mIsAutoPlaying = true;
            mAutoPlayHandler.sendEmptyMessageDelayed(WHAT_AUTO_PLAY, mAutoPalyTime);
        }
    }

    /**
     * 停止播放
     */
    public void stopAutoPlay() {
        if (mAutoPlayAble && mIsAutoPlaying) {
            mIsAutoPlaying = false;
            mAutoPlayHandler.removeMessages(WHAT_AUTO_PLAY);
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}








